/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.transformer.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.transformer.domain.FdsData;
import me.zhengjie.modules.transformer.domain.vo.FdsResponse;
import me.zhengjie.modules.transformer.domain.vo.MessageResponse;
import me.zhengjie.modules.transformer.query.service.MessageService;
import me.zhengjie.modules.transformer.query.service.RecordService;
import me.zhengjie.modules.transformer.service.FdsDataService;
import me.zhengjie.modules.transformer.service.FileService;
import me.zhengjie.modules.transformer.service.dto.FdsDataQueryCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.mail.Message;
import javax.servlet.http.HttpServletResponse;

/**
 * @author RuitaoWei
 * @website https://el-admin.vip
 * @date 2021-11-30
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "历史数据查询管理")
@RequestMapping("/api/fdsData")
public class FdsDataController {

    private final FdsDataService fdsDataService;

    @Autowired
    MessageService messageService;

    @Autowired
    FileService fileService;

    @Autowired
    RecordService recordService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('fdsData:list')")
    public void download(HttpServletResponse response, FdsDataQueryCriteria criteria) throws IOException {
        fdsDataService.download(fdsDataService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询历史数据查询")
    @ApiOperation("查询历史数据查询")
    @PreAuthorize("@el.check('fdsData:list')")
    public ResponseEntity<Object> query(FdsDataQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(fdsDataService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @GetMapping(value = "/record/list")
    @Log("查询导入批数")
    @ApiOperation("查询导入批数")
    public MessageResponse<Object> queryRecordList() {
        return new MessageResponse<>(recordService.queryAll().size(), recordService.queryAll());
    }

    @GetMapping(value = "/record/{recordid}")
    @Log("查询介质损耗因数")
    @ApiOperation("查询介质损耗因数")
    public FdsResponse<Object> queryRecords(@PathVariable("recordid") Integer recordid) {
        MessageResponse<Object> tanDelta = new MessageResponse<>(messageService.queryField("tan_delta", recordid).size(), messageService.queryField("tan_delta", recordid));
        MessageResponse<Object> eReal = new MessageResponse<>(messageService.queryField("e_real", recordid).size(), messageService.queryField("e_real", recordid));
        MessageResponse<Object> eImaginary = new MessageResponse<>(messageService.queryField("e_imaginary", recordid).size(), messageService.queryField("e_imaginary", recordid));
        MessageResponse<Object> cReal = new MessageResponse<>(messageService.queryField("c_real", recordid).size(), messageService.queryField("c_real", recordid));
        MessageResponse<Object> cImaginary = new MessageResponse<>(messageService.queryField("c_imaginary", recordid).size(), messageService.queryField("c_imaginary", recordid));
        return new FdsResponse<>(5, HttpStatus.OK.value(), "查询成功", tanDelta, eReal, eImaginary, cReal, cImaginary);
    }

    @PostMapping
    @Log("新增历史数据查询")
    @ApiOperation("新增历史数据查询")
    @PreAuthorize("@el.check('fdsData:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody FdsData resources) {
        return new ResponseEntity<>(fdsDataService.create(resources), HttpStatus.CREATED);
    }

    @PostMapping("/upload")
    @Log("上传文件")
    @ApiOperation("上传文件")
    public ResponseEntity<Object> uploadFile(@RequestParam MultipartFile file) {
        int i = fileService.receiveFile(file, "[FDSData]");
        return i == 1 ? new ResponseEntity<>("上传成功", HttpStatus.CREATED) : new ResponseEntity<>("上传失败", HttpStatus.CREATED);
    }

    @PostMapping(value = "/bat")
    @Log("批量新增历史数据")
    @ApiOperation("批量新增历史数据")
    public ResponseEntity<Object> create(@RequestBody List<FdsData> data) {
        return new ResponseEntity<>(fdsDataService.create(data), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改历史数据查询")
    @ApiOperation("修改历史数据查询")
    @PreAuthorize("@el.check('fdsData:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody FdsData resources) {
        fdsDataService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除历史数据查询")
    @ApiOperation("删除历史数据查询")
    @PreAuthorize("@el.check('fdsData:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Integer[] ids) {
        fdsDataService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}